/*
 * Administration.cpp
 *
 *  Created on: Apr 23, 2010
 *      Author: Sequoyha
 */

#include "Admin.h"
#include "Winery.h"
#include "WineryList.h"
#include "Menus.h"


/********************************************************************
 * AdminVerify
 *
 * Gets password from user and checks to see if valid. If valid allow
 * access to adminstration menu. If not valid returns user to main
 *
 *******************************************************************/
void AdminVerify()
{
	Administrator checkIn;		//Administrator class object
	int pass, badTry;			//pass = password input; badTry = bad tries

	//Init badTry to 0
	badTry = 0;

	//Loop to check password against passcode
	//If 3 bad attempts are made user is kicked to main
	while(!checkIn.passVerify() && badTry < 3)
	{
		//I/O password
		cout << "Administrative Options Menu\n";
		cout << "Please enter the passcode to continue: ";
		cin >> pass;
		checkIn.setPassword(pass);

		//Validating password
		if(checkIn.passVerify())
		{
			//Pass ok go to administration menu
			AdminMenu();
		}
		else
		{
			//Pass fail
			badTry++;
			cout << "Invalid Password entered, please try again.\n";
			cout << "You have " << 3-badTry
				 << " more attempt(s) to enter a valid password\n";
			cout << "or you will be returned to the main menu\n\n";
		}
	}
	if(badTry == 3)
	{
		cout << "You have entered 3 invalid attempts";
		cout << "- returning to Main Menu\n\n";
	}
	//Reset bool to prevent unauthorized access
	checkIn.resetVerify();
}
/********************************************************************
 * AddWinery
 *
 * Adds a winery to the list of wineries
 *
 * Pre condition: WineryList must already exist
 * Post condition: WineryList is 1 item larger
 *******************************************************************/
void AddWinery(WineryList &WineList)
{
	int distanceCV, numWines;
	string name;
	distFromHere newList;
	wines stockList;

	cin.ignore();

	//INPUT - gets info for new winery
	cout << "Please Enter the following information to add a winery "
		 << "to the database\n\n";
	cout << "Name: ";
	getline(cin, name);
	cout << "Distance from Canyon Villa: ";
	cin >> distanceCV;
	cin.ignore();
	cin.get();

	//Loop to add distance info
	for(int i = 0; i < Winery::totalWineries; i++)
	{
		float distance;
		cout << "Please enter the distance from Winery # " << i+1;
		cin >> distance;
		Distances newDistance(i+1, distance);	//Distance from winery struct
		//Add to list
		newList.push_back(newDistance);
	}
	cout << "Please enter the number of wines currently stocked: ";
	cin >> numWines;
	//Loop to add wines to stock
	for(int j = 0; j < numWines; j++)
	{
		WineInfo newWine = AddWine();
		stockList.push_back(newWine);
	}
	//New Winery object instance
	Winery newWinery(name, Winery::totalWineries,
			newList,distanceCV, numWines, stockList);
	//AddWinery to WineryList
	WineList.addNewWinery(newWinery);
}
/********************************************************************
 * DeleteWinery
 *
 * Deletes a winery to the list of wineries
 *
 * Pre condition: WineryList must already exist
 * Post condition: WineryList is 1 item smaller
 *******************************************************************/
void DeleteWinery(WineryList &WineList)
{
	int choice;
	char sure;

	//OUTPUT
	cout << "DELETE WINERY CONTROL\n\n";
	cout << "********PLEASE NOTE THAT FOR SECURITY ";
	cout << "PURPOSES ONLY 1 WINERY MAY BE DELETED AT A TIME********\n\n";
	WineList.printList();

	//INPUT- prompts user for winery to delete and checks again for security
	cout << "\n\nWhich Winery would you like to delete?(1-";
	cout << Winery::totalWineries << "): ";
	choice = MenuInput(10);
	cout << "Preparing to delete Winery #" << choice << endl;
	cout << "Are you sure you want to delete this Winery? ";
	cin >> sure;
	sure = toupper(sure);
	if(sure == 'Y')
	{
		//Delete winery from list
		cout << "Deleting Winery #" << choice << endl;
		WineList.deleteWinery(choice);
	}
	else
	{cout << "Aborting Delete action\n";}
}
/*********************************************************************
 * EDITWINERY
 *
 * Adds wines or edits the pricing of wines at the specified winery.
 *
 ********************************************************************/
void EditWinery(WineryList &CurrentList)
{
	int winery, selection;
	vector <Winery>::iterator wl;

	//OUTPUT
	CurrentList.printList();
	cout << "Which winery do you want to edit?: ";
	winery = MenuInput(Winery::totalWineries);
	wl = CurrentList.SearchList(winery);
	cout << wl->getWineryName() << endl;

	//INPUT - User selection of edit features
	cout << "Would you like to:\n1 - Add a wine\n or \n2 "
			<< "- Change the price of a wine?: ";
	selection = MenuInput(2);

	//ADDING A WINE TO THE WINERY
	if(selection == 1)
	{
		//Checking if wine stock is already at 10
		if(wl->getNumberOfWines() < 10)
		{
			char choice;
			do
			{
				//ADDING WINE
				wl->addNewWine(AddWine());
				cout << "Would you like to add another?(Y or N) ";
				cin >> choice;
				choice = toupper(choice);
			}while(choice =='Y');
		}
		//ERROR OUTPUT
		else
		{cout << "There are already 10 wines at this winery\n";
		cout << "Aborting add\n";
		}
	}
	//CHANGING PRICING OF A WINE
	else
	{
		char choice2;
		do
		{
			//CHANGING PRICING OF A WINE
			wl->changePricing();
			cout << "Would you like to edit another? ";
			cin >> choice2;
			choice2 = toupper(choice2);
		}while(choice2 =='Y');

	}

}

//AddWine subroutine for Edit function
WineInfo AddWine()
{
	//Need code to search for correct winery in WineList
	string wName;
	int vintage;
	float price;

	cin.ignore();
	cout << "Enter the name of the wine: ";
	getline(cin,wName);
	cout << "Enter the vintage of the wine: ";
	cin >> vintage;
	cout << "Enter the price of the wine: ";
	cin >> price;
	WineInfo newWine(wName, vintage, price);
	return newWine;
	//Need to call addNewWine function from winery class
}
